/**
 * 作者: wgx
 * 日期: 2025-03-12 17:39:32
 * 备注: 用户物资争夺模块数据的物资数量
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.server.game.base.CommonUtils;
import com.motu.monstercity.server.game.base.ProtoDataUtils;
import com.motu.monstercity.server.game.commondata.act.ActTopRanking;
import com.motu.monstercity.server.game.commondata.bag.ItemId;
import com.motu.monstercity.server.game.commondata.maintask.MainTaskConstant;
import com.motu.monstercity.server.game.commondata.rank.RankConstant;
import com.motu.monstercity.server.game.commondata.skill.SkillConstant;
import com.motu.monstercity.server.game.log.MotuLogManager;
import com.motu.monstercity.server.game.logic.MainTaskManager;
import com.motu.monstercity.server.game.logic.RankActManager;
import com.motu.monstercity.server.game.logic.RankManager;
import com.motu.monstercity.server.game.logic.SkillManager;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import io.vertx.core.json.JsonObject;

import java.util.List;

public class UserSnatchScore extends UserSnatchScoreModel {
    public UserSnatchScore() {
        super();
    }

    public JsonObject insert() {
        putResetTimeWeek(TimeUtils.getCurTime());
        return super.insert();
    }

    public void reset() {
        long curTime = TimeUtils.getCurTime();
        if (!TimeUtils.isSameWeek(curTime, getResetTimeWeek())) {
            putScore(0);// 物资争夺分数每周重置
            putResetTimeWeek(curTime);
            update();
        }
    }


    /**
     * 记录日志并增加积分,更新榜单，更新个人成就
     * @param pbUserData
     * @param userInfo
     * @param partnerList 上阵的干员，干员技能会影响 该英雄在物资争夺出战時我方获得的物资@
     * @param num
     * @param params
     */
    public int addScore(PBUserData.Builder pbUserData, UserInfo userInfo, List<UserPartner> partnerList, int num, Object... params) {
        if (num == 0) {
            return 0;
        }

        int skillAdd = 0;
        if (partnerList != null) {
            for (UserPartner userPartner : partnerList) {
                skillAdd += userPartner.countModuleSkillAdd(SkillConstant.EFFECT_TYPE_26, 0);// 该英雄在物资争夺出战時我方获得的物资@  从单个干员取
            }
        }
        skillAdd += SkillManager.getSkillMoudleAdd(userInfo.getId(), SkillConstant.EFFECT_TYPE_30, 0);// 物资争夺物资获取@  百分比加成 从所有秘书上取
        int newNum = (int) CommonUtils.countPerValue(num, skillAdd);// 实际获得的物资数量

        long balance = getScore();// 当前余额
        long dbid = 0;
        MotuLogManager.logItemIncome(userInfo, ItemId.FIGHT_SNATCH_SCORE, dbid, balance, newNum, params);
        super.addScore(newNum);
        ProtoDataUtils.updatePBUserData(pbUserData, this);
        String dateKey = RankManager.getDateKey(RankConstant.SERVER_RANK_SNATCH);//这周一的0点0分0秒
        RankManager.updateRank(RankConstant.SERVER_RANK_SNATCH, userInfo.getServerId(), userInfo.getId(), getScore(), dateKey);
        MainTaskManager.updateUserHonor(pbUserData, userInfo, MainTaskConstant.HONOR_TYPE_101, (int)getScore(), false);// 全量更新物资争夺的个人荣誉进度
        RankActManager.addUserrank(userInfo.getServerId(), ActTopRanking.RANK_TYPE_SNATCH_EARN, userInfo, getScore(), false);
        RankActManager.addUserCrossRank(userInfo, userInfo.getServerId(), ActTopRanking.RANK_TYPE_SNATCH_EARN, getScore(), false);

        return newNum;// 返回实际增加的数量
    }

    /**
     * 给对方扣物资
     * @param userInfo
     * @param partnerList 我方上阵的干员， 该英雄在物资争夺出战時敌方 会增加对方 損失的物资
     * @param num
     * @param params
     */
    public void subScore(UserInfo userInfo, List<UserPartner> partnerList, int num, Object... params) {
        if (num == 0) {
            return;
        }
        int skillAdd = 0;
        if (partnerList != null) {
            for (UserPartner userPartner : partnerList) {
                skillAdd += userPartner.countModuleSkillAdd(SkillConstant.EFFECT_TYPE_27, 0);// 该英雄在物资争夺出战時敌方 会增加对方 損失的物资 @  从单个干员取
            }
        }
        int newNum = (int) CommonUtils.countPerValue(num, skillAdd);// 实际要扣除的物资数量

        long balance = getScore();// 当前余额
        long dbid = 0;
        MotuLogManager.logItemConsume(userInfo, ItemId.FIGHT_SNATCH_SCORE, dbid, balance, newNum, params);
        super.addScore(-newNum);
        String dateKey = RankManager.getDateKey(RankConstant.SERVER_RANK_SNATCH);//这周一的0点0分0秒
        RankManager.updateRank(RankConstant.SERVER_RANK_SNATCH, userInfo.getServerId(), userInfo.getId(), getScore(), dateKey);
    }
}
